﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PSI.Models;
using PSI.Services;
using System.Text;

namespace PSI.Services.TMS
{
    public class WinXinResServer:PSIBaseService
    {
        public List<WeiXinRes> GetimportByorder(string id)
        {
            var result = new List<WeiXinRes>();
            var sql = new StringBuilder("select * from tms_weixin_resverimport where v_orderID=@id");
            var p = CreateDbParameters();
            p.AddWithValue("id", id);
            Query(sql.ToString(), p, reader =>
            {
                var temp = new WeiXinRes
                {
                    id = reader.GetGuid(reader.GetOrdinal("id")),
                    orderID = reader.GetGuid(reader.GetOrdinal("v_orderID")),
                    carnumber = reader.GetString(reader.GetOrdinal("v_carnumber")),
                    owner = reader.GetString(reader.GetOrdinal("v_owner")),
                    ownerTel = reader.GetString(reader.GetOrdinal("v_owner_tel")),
                    ownerPrice = reader.GetDecimal(reader.GetOrdinal("f_owner_price")),
                    driver = reader.GetString(reader.GetOrdinal("v_driver")),
                    driverTel = reader.GetString(reader.GetOrdinal("v_driver_tel")),
                    ResverTime = reader.GetDateTime(reader.GetOrdinal("d_resver_time")),
                    Lastdt = reader.GetDateTime(reader.GetOrdinal("d_lastdt")),
                    ImorOut = 0,
                    Isselect = reader.GetInt32(reader.GetOrdinal("i_isselect"))
                };
                result.Add(temp);
            });
            return result;
        
        }

        public List<WeiXinRes> GetexportByorder(string id)
        {
            var result = new List<WeiXinRes>();
            var sql = new StringBuilder("select * from tms_weixin_resveroutport where v_orderID=@id");
            var p = CreateDbParameters();
            p.AddWithValue("id", id);
            Query(sql.ToString(), p, reader =>
            {
                var temp = new WeiXinRes
                {
                    id = reader.GetGuid(reader.GetOrdinal("id")),
                    orderID = reader.GetGuid(reader.GetOrdinal("v_orderID")),
                    carnumber = reader.GetString(reader.GetOrdinal("v_carnumber")),
                    owner = reader.GetString(reader.GetOrdinal("v_owner")),
                    ownerTel = reader.GetString(reader.GetOrdinal("v_owner_tel")),
                    ownerPrice = reader.GetDecimal(reader.GetOrdinal("f_owner_price")),
                    driver = reader.GetString(reader.GetOrdinal("v_driver")),
                    driverTel = reader.GetString(reader.GetOrdinal("v_driver_tel")),
                    ResverTime = reader.GetDateTime(reader.GetOrdinal("d_resver_time")),
                    Lastdt = reader.GetDateTime(reader.GetOrdinal("d_lastdt")),
                    CntNum = reader.GetString(reader.GetOrdinal("v_cntnum")),
                    ImorOut = 0,
                    Isselect = reader.GetInt32(reader.GetOrdinal("i_isselect"))
                };
                result.Add(temp);
            });
            return result;
        }

        public WeiXinRes GetSinglebyID(string id,int type)
        {
            var table = type == 1 ? "tms_weixin_resverimport" : "tms_weixin_resveroutport";
            var result = new WeiXinRes();
            var sql = new StringBuilder("select * from "+table+" where id=@id");
            var p = CreateDbParameters();
            p.AddWithValue("id", id);
            Query(sql.ToString(), p, reader =>
            {
                  result.id = reader.GetGuid(reader.GetOrdinal("id"));
                  result.orderID = reader.GetGuid(reader.GetOrdinal("v_orderID"));
                  result.carnumber = reader.GetString(reader.GetOrdinal("v_carnumber"));
                  result.owner = reader.GetString(reader.GetOrdinal("v_owner"));
                  result.ownerTel = reader.GetString(reader.GetOrdinal("v_owner_tel"));
                  result.ownerPrice = reader.GetDecimal(reader.GetOrdinal("f_owner_price"));
                  result.driver = reader.GetString(reader.GetOrdinal("v_driver"));
                  result.driverTel = reader.GetString(reader.GetOrdinal("v_driver_tel"));
                  result.ResverTime = reader.GetDateTime(reader.GetOrdinal("d_resver_time"));
                  result.ImorOut = 0;
                  result.Isselect = reader.GetInt32(reader.GetOrdinal("i_isselect"));
            });
            return result;
        }

        public bool ImportResDecision(string id)
        {
            try {

                var inmportdemil = GetSinglebyID(id,1);
                WithTransaction(() =>
                {
                    var sql = new StringBuilder("update tms_weixin_resverimport set i_isselect=1 where id=@id");
                    var p = CreateDbParameters();
                    p.AddWithValue("id", id);
                    Execute(sql.ToString(), p);

                    var sqls = new StringBuilder("update tms_weixin_resverimport set i_isselect=2 where id!=@id and v_orderID=@orderID");

                    p = CreateDbParameters();
                    p.AddWithValue("id", id);
                    p.AddWithValue("orderID", inmportdemil.orderID);

                    Execute(sqls.ToString(), p);

                    sql = new StringBuilder("update tms_in_consignation_detail set f_status='已派车',f_truck_num=@tnum,f_driver=@tdriver,f_driver_tel=@tdriverTel,f_truck_dt=@truckdt,f_truck_company=@truckcompany,i_isweixin=3 where id=@id");
                    p = CreateDbParameters();
                    p.AddWithValue("id", inmportdemil.orderID);
                    p.AddWithValue("tnum",inmportdemil.carnumber);
                    p.AddWithValue("tdriver", inmportdemil.driver);
                    p.AddWithValue("truckcompany", inmportdemil.owner);
                    p.AddWithValue("truckdt", DateTime.Now);
                    p.AddWithValue("tdriverTel", inmportdemil.driverTel);
                    Execute(sql.ToString(),p);

                });
                return true;
            }
            catch(Exception ex){
                return false;
            }
        
        }

        public bool ExportResDecision(string id)
        {
            try
            {
                var inmportdemil = GetSinglebyID(id,2);
                WithTransaction(() =>
                {
                    var sql = new StringBuilder("update tms_weixin_resveroutport set i_isselect=1 where id=@id");
                    var p = CreateDbParameters();
                    p.AddWithValue("id", id);
                    Execute(sql.ToString(), p);

                    var sqls = new StringBuilder("update tms_weixin_resveroutport set i_isselect=2 where id!=@id and v_orderID=@orderID");

                    p = CreateDbParameters();
                    p.AddWithValue("id", id);
                    p.AddWithValue("orderID",inmportdemil.orderID);

                    Execute(sqls.ToString(),p);

                    sql = new StringBuilder("update tms_out_consignation_detail set f_status='已派车', f_truck_num=@tnum,f_driver=@tdriver,f_driver_tel=@tdriverTel,f_truck_dt=@truckdt,f_truck_company=@truckcompany,i_isweixin=3 where id=@id");
                    p = CreateDbParameters();
                    p.AddWithValue("id", inmportdemil.orderID);
                    p.AddWithValue("tnum", inmportdemil.carnumber);
                    p.AddWithValue("truckcompany", inmportdemil.owner);
                    p.AddWithValue("tdriver", inmportdemil.driver);
                    p.AddWithValue("truckdt", DateTime.Now);
                    p.AddWithValue("tdriverTel", inmportdemil.driverTel);
                    Execute(sql.ToString(), p);

                });
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }

        }
    }
}